/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.user;

import java.util.List;
import queenb.net.entitybean.K250;
import queenb.web.controller.QueenbController;
import queenb.web.store.K250Store;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class K250Controller  extends QueenbController{
    private int pnk250;
    private String pvk251;
    private K250Store k250Store;
    private boolean flag;
    
    public K250Controller()
    {
        Object k250Object = getRequest().getParameter("pk250");
        if(getRequest().getParameter("action") != null)
        {
            String action = getRequest().getParameter("action");
            if(action.equals("add"))
            {
                flag = true;
            }
            if(action.equals("edit"))
            {
                flag = false;
                this.pnk250 = Util.parseInt(k250Object);
                getListK250DetailEdit(pnk250);
            }
        }
        
    }
    
    public List<K250> getListK250All()
    {
        try{
            k250Store = new K250Store();
            List<K250> listK250All = k250Store.getListK250All(99, 7, getLogin().getUserName());
            return listK250All;
        }catch(Exception ex)
        {
            System.out.println("ERROR : " + ex.getMessage());
            return null;
        }
        
    }
    
    public void getListK250DetailEdit(int pk250i)
    {
        this.pnk250 = pk250i;
        try{
            List<K250> listK250Detail = getListK250All();
            if(listK250Detail.size() > 0)
            {
                for(int i = 0; i < listK250Detail.size(); i++)
                {
                    if(listK250Detail.get(i).getPk250() == pnk250)
                    {
                        this.pvk251 = listK250Detail.get(i).getKv251();
                        break;
                    }
                }
            }
        }catch(Exception ex)
        {
              System.out.println("ERROR : " + ex.getMessage());
        }
    }
    
    public void updateK250()
    {
        k250Store = new K250Store();
        if(flag == true)
        {
            int count = k250Store.updateK250(pnk250, pvk251, 7, 99, getLogin().getUserName());
             if (count > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        }else{
            int count = k250Store.updateK250(pnk250, pvk251, 7, 99, getLogin().getUserName());
             if (count > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        }
        sendRedirectUrlTab("setting.jsp?tab=header3&"+Util.convertMD5(getRequest().getRequestURI()));
    }
    
    public int deleteK250(int id)
    {
        k250Store = new K250Store();
        if(id != 0)
        {
            int rows = k250Store.deleteK250(id, getLogin().getUserName());
            return rows;
        }else
        {
            return 0;
        }
            
    }
    /**
     * @return the pnk250
     */
    public int getPnk250() {
        return pnk250;
    }

    /**
     * @param pnk250 the pnk250 to set
     */
    public void setPnk250(int pnk250) {
        this.pnk250 = pnk250;
    }

    /**
     * @return the pvk251
     */
    public String getPvk251() {
        return pvk251;
    }

    /**
     * @param pvk251 the pvk251 to set
     */
    public void setPvk251(String pvk251) {
        this.pvk251 = pvk251;
    }

    /**
     * @return the flag
     */
    public boolean isFlag() {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(boolean flag) {
        this.flag = flag;
    }
    
}
